package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.frame.work.common.constant.CallType.TYPE_GET;
import static com.frame.work.common.constant.CallType.TYPE_SET;

/**
 *液晶场景控制器
 */
@Data
public class YeJingChangJingKongZhiQi_0FAA8A03 extends BaseFrame {

    private Integer switchState;
    private String sceneNumber;


    public YeJingChangJingKongZhiQi_0FAA8A03(String gatewayMac, String data, String type) {
        //将基础数据进行处理
        super(gatewayMac, data);
        if (TYPE_GET.equals(type)) {
            YeJingChangJingKongZhiQi_0FAA8A03Get(gatewayMac, data);
        }
        if (TYPE_SET.equals(type)) {
            YeJingChangJingKongZhiQi_0FAA8A03Set(gatewayMac, data);
        }

    }

    public void YeJingChangJingKongZhiQi_0FAA8A03Get(String gatewayMac, String data) {

        //处理此设备特有的数据
        this.setSwitchState(-100);
        this.setSceneNumber(StringUtils.substring(data, data.length() - 2, data.length()));

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("sceneNumber", sceneNumber);
        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }


    public void YeJingChangJingKongZhiQi_0FAA8A03Set(String gatewayMac, String data) {

        this.setOdIndex("");
        this.setDeviceType("");
        this.setProductType("");
        this.setSwitchState(-100);


        this.setDeviceMac(StringUtils.substring(data, 2, 18));
        String afterOdIndex = StringUtils.substring(data, 22, 24);
        String state = StringUtils.substring(data, 24, 26);

        SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
        List<Map> list = testMapper.getDevice3(this.getDeviceMac());

        String nightLightTime = null;
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if ("nightLightTime".equals(list.get(i).get("actions_key"))) {
                    nightLightTime = (String) list.get(i).get("actions_value");
                }
            }
        }
        String way1 = "01";
        String way2 = "02";
        String way3 = "03";
        String way4 = "04";
        String way5 = "05";
        String way6 = "06";
        String way7 = "07";
        String way8 = "08";
        if (nightLightTime != null) {


            if (way1.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 1);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way2.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 2);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way3.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 3);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way4.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 4);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way5.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 5);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way6.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 6);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way7.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 7);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way8.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 8);
                actions.put("state", state);
                actions.put("nightLightTime", nightLightTime);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            }


        } else {

            if (way1.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 1);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way2.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 2);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way3.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 3);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way4.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 4);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way5.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 5);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way6.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 6);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way7.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 7);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            } else if (way8.equals(afterOdIndex)) {
                //将actions信息放入json对象中
                Map<String, Object> actions = new HashMap(16);
                actions.put("wayNumber", 8);
                actions.put("state", state);

                this.setActions(actions);


                //处理好的json对象
                String json = JSONObject.toJSONString(this);
                //将数据发送到mqtt队列中
                JsonToMqtt.mqttLianpushScene(gatewayMac, json);

            }
        }


    }


}
